<template>
  <div class="app-container">
    <PageSearch :showSearch.sync="showSearch" :getList="getList" :handleQuery="handleQuery" :resetQuery="resetQuery">
      <template #form>
        <el-form :model="queryParams" v-show="showSearch" ref="queryForm" size="small" :inline="true">
          <el-row>
            <el-form-item prop="skuNo">
              <el-input
                v-model="queryParams.skuNo"
                :placeholder="$t('sku编号')"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <!-- 库位 -->
            <el-form-item prop="spNo">
              <el-input
                :placeholder="$t('库位号')"
                v-model="queryParams.spNo"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item prop="sellerName">
              <el-input
                :placeholder="$t('卖家名称')"
                v-model="queryParams.sellerName"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>

            <el-form-item prop="applyRestrict">
              <el-select
                :placeholder="$t('是否限制')"
                clearable
                @change="handleQuery"
                v-model="queryParams.applyRestrict"
              >
                <el-option :value="1" :label="$t('限制')" />
                <el-option :value="2" :label="$t('不限制')" />
              </el-select>
            </el-form-item>
            <el-form-item prop="skuNameCn">
              <el-input
                v-model="queryParams.skuNameCn"
                :placeholder="$t('中文名')"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item prop="skuNameJp">
              <el-input
                v-model="queryParams.skuNameJp"
                :placeholder="$t('日文名')"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item prop="spNo">
              <treeselect
                size="mini"
                :appendToBody="true"
                v-model="queryParams.classId"
                :options="menuOptions"
                :show-count="true"
                :placeholder="$t('商品分类')"
              />
            </el-form-item>
            <el-form-item label-width="120px" prop="gtNum">
              <el-input
                :placeholder="$t('可用库存大于')"
                v-model="queryParams.gtNum"
                type="number"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label-width="120px" prop="ltNum">
              <el-input
                :placeholder="$t('可用库存小于')"
                v-model="queryParams.ltNum"
                type="number"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item prop="janCode">
              <el-input
                v-model="queryParams.janCode"
                :placeholder="$t('jan编码')"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-row>
        </el-form>
      </template>
      <template #actions>
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              v-hasPermi="['distribution:skuSeller:exportSkuSeller']"
              @click="handleExport"
            >
              {{ $t('导出') }}
            </el-button>
          </el-col>
        </el-row>
      </template>
      <template #pagination>
        <pagination
          v-show="pageTotal > 0"
          :total="pageTotal"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </template>
    </PageSearch>

    <el-table
      @row-click="(row) => $rowClick(row, clickedRows)"
      :row-class-name="({ row }) => $tableRowClassName(row, clickedRows)"
      :height="calTableHeight()"
      v-loading="loading"
      ref="TableRef"
      :data="tableData"
      row-key="menuId"
    >
      <el-table-column align="center" show-overflow-tooltip width="150" prop="sellerName" :label="$t('卖家名称')" />
      <el-table-column align="center" show-overflow-tooltip prop="skuNo" :label="$t('sku编号')" width="150">
        <template #header="{ column }">
          <div class="sort-item">
            <div @click="resetSortQuery(column)">
              {{ column.label }}
            </div>
            <div class="sort-item-icon">
              <i
                class="el-icon-caret-top"
                @click="setSortQuery(column, 'asc')"
                :style="setSortIconStyle(column, 'asc')"
              />
              <i
                @click="setSortQuery(column, 'desc')"
                :style="setSortIconStyle(column, 'desc')"
                class="el-icon-caret-bottom"
              />
            </div>
          </div>
        </template>
        <template slot-scope="scope">
          <div @click="handleEdit(scope.row)" style="cursor: pointer; color: #1890ff">
            {{ scope.row.skuNo }}
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip prop="spNo" :label="$t('库位号')" width="150">
        <template #header="{ column }">
          <div class="sort-item">
            <div @click="resetSortQuery(column)">
              {{ column.label }}
            </div>
            <div class="sort-item-icon">
              <i
                class="el-icon-caret-top"
                @click="setSortQuery(column, 'asc')"
                :style="setSortIconStyle(column, 'asc')"
              />
              <i
                @click="setSortQuery(column, 'desc')"
                :style="setSortIconStyle(column, 'desc')"
                class="el-icon-caret-bottom"
              />
            </div>
          </div>
        </template>
      </el-table-column>

      <el-table-column :label="$t('主图')" prop="imgUrl" align="center">
        <template slot-scope="scope">
          <el-image
            fit="contain"
            style="width: 50px; height: 50px"
            :key="Math.random()"
            v-hover="scope.row.imgUrl"
            :src="scope.row.imgUrl + '?x-oss-process=image/resize,w_100/quality,q_70'"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip prop="skuNameCn" :label="$t('中文名')">
        <template #header="{ column }">
          <div class="sort-item">
            <div @click="resetSortQuery(column)">
              {{ column.label }}
            </div>
            <div class="sort-item-icon">
              <i
                class="el-icon-caret-top"
                @click="setSortQuery(column, 'asc')"
                :style="setSortIconStyle(column, 'asc')"
              />
              <i
                @click="setSortQuery(column, 'desc')"
                :style="setSortIconStyle(column, 'desc')"
                class="el-icon-caret-bottom"
              />
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" width="150" show-overflow-tooltip prop="skuNameJp" :label="$t('日文名')">
        <template #header="{ column }">
          <div class="sort-item">
            <div @click="resetSortQuery(column)">
              {{ column.label }}
            </div>
            <div class="sort-item-icon">
              <i
                class="el-icon-caret-top"
                @click="setSortQuery(column, 'asc')"
                :style="setSortIconStyle(column, 'asc')"
              />
              <i
                @click="setSortQuery(column, 'desc')"
                :style="setSortIconStyle(column, 'desc')"
                class="el-icon-caret-bottom"
              />
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip prop="stock" :label="$t('可用库存')">
        <template #header="{ column }">
          <div class="sort-item">
            <div @click="resetSortQuery(column)">
              {{ column.label }}
            </div>
            <div class="sort-item-icon">
              <i
                class="el-icon-caret-top"
                @click="setSortQuery(column, 'asc')"
                :style="setSortIconStyle(column, 'asc')"
              />
              <i
                @click="setSortQuery(column, 'desc')"
                :style="setSortIconStyle(column, 'desc')"
                class="el-icon-caret-bottom"
              />
            </div>
          </div>
        </template>
        <template slot-scope="{ row }">
          <span
            style="cursor: pointer"
            :style="{
              color: row.stock > 0 ? 'red' : '',
            }"
          >
            {{ row.stock }}
          </span>
        </template>
      </el-table-column>
      <!-- 最新申请价格 -->
      <el-table-column align="center" show-overflow-tooltip prop="price" :label="$t('最新申请价格')" width="150">
        <template slot-scope="{ row }">
          <span>{{ (+row.price || 0).toFixed() }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip :label="$t('是否限制')">
        <template slot-scope="scope">
          <div v-if="scope.row.applyRestrict == 1">
            {{ $t('限制') }}
          </div>
          <div v-if="scope.row.applyRestrict == 2">
            {{ $t('不限制') }}
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip prop="weekSales" :label="$t('周销量')" />
      <el-table-column align="center" show-overflow-tooltip prop="monthSales" :label="$t('月销量')" />
      <el-table-column align="center" show-overflow-tooltip prop="totalSales" :label="$t('总销量')" />
      <el-table-column align="center" show-overflow-tooltip prop="cloudStock" width="150" :label="$t('可申请数量')">
        <template slot-scope="{ row }">
          <span
            :style="{
              color: row.cloudStock > 0 ? 'red' : '',
            }"
          >
            {{ row.cloudStock }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip prop="cloudPrice" :label="$t('云价格')" />

      <el-table-column align="center" show-overflow-tooltip prop="isApplyNum" width="150" :label="$t('申请中数量')" />
      <el-table-column fixed="right" :label="$t('操作')" width="200" align="center">
        <template slot="header">
          <div>
            {{ $t('操作') }}
            <el-tooltip effect="dark" placement="top-start">
              <div slot="content">
                <p>{{ $t('申请限制') }}：</p>
                <p>
                  {{ $t('1.已经申请过的sku没卖完或者没卖超过申请数量一半，禁止继续申请') }}
                </p>
                <p>
                  {{
                    $t(
                      '例如：J-(00000006)申请了20，如果付款数量没超过10个就不能继续申请，付款数量会在销售、提前付款、周期结账时更新。',
                    )
                  }}
                </p>
                <p>
                  {{
                    $t(
                      '2.申请后如果觉得卖不完可以退回库存，可以选择数量，但是一个申请只能退回一次，并且退回数量大于一半会在卖家SKU库存表标记限制',
                    )
                  }}
                </p>
                <p>
                  {{ $t('被标记限制的sku申请时，只能申请月销售数量') }}
                </p>
                <p>
                  {{
                    $t(
                      '例如：J-(00000006)申请了20，如果退回了10个就会被标记限制，限制后如果还想申请，只能申请等于或小于该sku的月销量的数量，或者向管理员提出解除限制',
                    )
                  }}
                </p>
              </div>
              <i style="cursor: pointer" class="el-icon-info"></i>
            </el-tooltip>
          </div>
        </template>
        <template slot-scope="scope">
          <el-popconfirm
            v-hasPermi="['system:skuSellerApply:closeLimit']"
            :title="$t('手动解除申请限制')"
            @confirm="submitCloseLimit(scope.row)"
          >
            <el-button :disabled="scope.row.applyRestrict !== 1" slot="reference" type="text">
              {{ $t('手动解除申请限制') }}
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="pageTotal > 0"
      :total="pageTotal"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- sku详情/修改 -->
    <Skucontmoduler @relaod="relaod" ref="Skucontmoduler"></Skucontmoduler>
  </div>
</template>

<script>
import { getSkuSellerList, closeLimit } from '@/api/distribution/sku/SellerSku'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  activated() {
    this.$nextTick(() => this.$refs['TableRef']?.doLayout())
  },
  name: 'SellerSkuPlatform',
  components: {
    Treeselect,
  },
  data() {
    return {
      /**点击的表格项 */
      clickedRows: [],
      /**商品分类菜单树选项 */
      menuOptions: [],
      showSearch: false,
      tableData: [],
      /**数据总条数 */
      pageTotal: 0,
      loading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        value2: 1,
      },
    }
  },
  watch: {
    'queryParams.pageNum': function (val) {
      this.clickedRows = []
    },
  },
  created() {
    this.getList()
    this.$getSkuClassList().then((res) => {
      this.menuOptions = res.menuOptions
    })
  },
  methods: {
    handleExport() {
      this.download(
        'distribution/skuSeller/exportSkuSeller',
        {
          ...this.queryParams,
        },
        `exportSkuSellerList_${new Date().getTime()}.xlsx`,
      )
    },
    relaod() {
      this.getList()
    },
    handleEdit(row) {
      this.$refs.Skucontmoduler.setSkuInfo(row)
      this.$refs.Skucontmoduler.checkNumber('basicInformation')
    },
    submitCloseLimit(value) {
      closeLimit({
        id: value.id,
      }).then((res) => {
        if (res.code == 200) {
          this.$message({
            type: 'success',
            message: res.msg,
          })
        }
        this.getList()
      })
    },
    /** 查询菜单列表 */
    getList() {
      this.loading = true
      getSkuSellerList(this.queryParams).then((res) => {
        this.tableData = res.rows
        this.pageTotal = res.total
        this.loading = false
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.value1 = null
      Object.keys(this.queryParams).forEach((key) => {
        this.queryParams[key] = null
      })
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10

      this.getList()
    },
  },
}
</script>
<style lang="scss" scoped></style>
